package com.bdqn.vueboot.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.bdqn.vueboot.config.R;
import com.bdqn.vueboot.dto.BuildingDto;
import com.bdqn.vueboot.entity.Building;
import com.bdqn.vueboot.entity.Matters;
import com.bdqn.vueboot.service.BuildingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 楼宇维修(Building)表控制层
 *
 * @author makejava
 * @since 2022-12-03 09:09:35
 */
@RestController
@RequestMapping("building")
@CrossOrigin
public class BuildingController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private BuildingService buildingService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param building 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Building> page, Building building) {
        return R.success(this.buildingService.page(page, new QueryWrapper<>(building)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.success(this.buildingService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param building 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Building building) {
        return R.success(this.buildingService.save(building));
    }

    /**
     * 修改数据
     *
     * @param building 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Building building) {
        return R.success(this.buildingService.updateById(building));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.success(this.buildingService.removeByIds(idList));
    }
//    @RequestParam(value = "buildingStartdata", required = false) String buildingStartdatas,
//    @RequestParam(value = "buildingStartdata2", required = false) String buildingStartdatas2,
//    @RequestParam(value = "buildingAddress", required = false) String buildingAddress,
//    @RequestParam(value = "buildingContactsName", required = false) String buildingContactsName,
//    @RequestParam(value = "buildingMattersId", required = false) Integer buildingMattersId
    @RequestMapping(value = "/page")
    public R pageList(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                      @RequestParam(value = "limit", required = false) Integer limit,
                      BuildingDto building
     ){
        IPage page=new Page(pageIndex,limit);
        IPage page1 = buildingService.pageList(page,building);
        return R.success(page1);
    }
    @PostMapping("/add")
    public R add(@RequestBody BuildingDto building) {
        int i= buildingService.add(building);
        return R.success(i);
    }


    @GetMapping("/deleteById")
    public R deleteById( @RequestParam(value = "buildingId", required = false) Long buildingId) {
        return R.success(this.buildingService.removeById(buildingId));
    }
}

